#define FILESCANNER_EXPORTS
#include "../Common/ScannerInterface.h"
#include "FileScanner.h"
#include <map>
#include <mutex>

// 全局管理器，用于管理FileScanner实例
class ScannerManager {
private:
    std::map<HANDLE, std::unique_ptr<FileScanner>> scanners;
    std::mutex managerMutex;
    static ScannerManager* instance;
    static std::mutex instanceMutex;
    
    ScannerManager() = default;
    
public:
    static ScannerManager& getInstance() {
        std::lock_guard<std::mutex> lock(instanceMutex);
        if (!instance) {
            instance = new ScannerManager();
        }
        return *instance;
    }
    
    HANDLE createScanner() {
        std::lock_guard<std::mutex> lock(managerMutex);
        auto scanner = std::make_unique<FileScanner>();
        HANDLE handle = reinterpret_cast<HANDLE>(scanner.get());
        scanners[handle] = std::move(scanner);
        return handle;
    }
    
    void destroyScanner(HANDLE handle) {
        std::lock_guard<std::mutex> lock(managerMutex);
        auto it = scanners.find(handle);
        if (it != scanners.end()) {
            it->second->stopScan(); // 确保停止扫描
            scanners.erase(it);
        }
    }
    
    FileScanner* getScanner(HANDLE handle) {
        std::lock_guard<std::mutex> lock(managerMutex);
        auto it = scanners.find(handle);
        return (it != scanners.end()) ? it->second.get() : nullptr;
    }
};

ScannerManager* ScannerManager::instance = nullptr;
std::mutex ScannerManager::instanceMutex;

// DLL入口点
#ifdef _WIN32
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
#endif

// 导出函数实现
extern "C" {

SCANNER_API HANDLE CreateScanner() {
    try {
        return ScannerManager::getInstance().createScanner();
    } catch (...) {
        return nullptr;
    }
}

SCANNER_API void DestroyScanner(HANDLE scanner) {
    if (scanner) {
        try {
            ScannerManager::getInstance().destroyScanner(scanner);
        } catch (...) {
            // 忽略异常，确保函数不会抛出
        }
    }
}

SCANNER_API bool StartScan(HANDLE scanner, const wchar_t* folderPath) {
    if (!scanner || !folderPath) {
        return false;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            std::wstring path(folderPath);
            return fs->startScan(path);
        }
    } catch (...) {
        // 异常处理
    }
    
    return false;
}

SCANNER_API void StopScan(HANDLE scanner) {
    if (!scanner) {
        return;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            fs->stopScan();
        }
    } catch (...) {
        // 忽略异常
    }
}

SCANNER_API bool IsScanning(HANDLE scanner) {
    if (!scanner) {
        return false;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            return fs->isScanning();
        }
    } catch (...) {
        // 异常处理
    }
    
    return false;
}

SCANNER_API void SetProgressCallback(HANDLE scanner, ProgressCallbackC callback) {
    if (!scanner) {
        return;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            fs->setProgressCallbackC(callback);
        }
    } catch (...) {
        // 忽略异常
    }
}

SCANNER_API void SetCompleteCallback(HANDLE scanner, CompleteCallbackC callback) {
    if (!scanner) {
        return;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            fs->setCompleteCallbackC(callback);
        }
    } catch (...) {
        // 忽略异常
    }
}

SCANNER_API int GetScanStatus(HANDLE scanner) {
    if (!scanner) {
        return static_cast<int>(ScanStatus::Error);
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            return static_cast<int>(fs->getStatus());
        }
    } catch (...) {
        // 异常处理
    }
    
    return static_cast<int>(ScanStatus::Error);
}

SCANNER_API uint64_t GetTotalFilesScanned(HANDLE scanner) {
    if (!scanner) {
        return 0;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            return fs->getTotalFilesScanned();
        }
    } catch (...) {
        // 异常处理
    }
    
    return 0;
}

SCANNER_API uint64_t GetTotalBytesScanned(HANDLE scanner) {
    if (!scanner) {
        return 0;
    }
    
    try {
        FileScanner* fs = ScannerManager::getInstance().getScanner(scanner);
        if (fs) {
            return fs->getTotalBytesScanned();
        }
    } catch (...) {
        // 异常处理
    }
    
    return 0;
}

} // extern "C"